<template>
  <div>
    <button @click="add">去首页</button>
  </div>
</template>
<script>
export default {
  data() {
    return {}
  },
  methods: {
    add() {
      this.$router.push({
        name: 'index'
      })
    },
    handler(event) {
      console.log(`handler`)
      history.pushState(null, null, location.href)
      //alert(`位置：${document.location}，状态：${JSON.stringify(event.state)}`)
      console.log(`位置：${document.location}，状态：${JSON.stringify(event.state)}`)
    }
  },
  beforeRouteLeave() {
    const answer = window.confirm('Do you really want to leave? you have unsaved changes!')
    if (!answer) return false
  },
  mounted() {
    window.addEventListener('popstate', this.handler)
  },
  unmounted() {
    window.removeEventListener('popstate', this.handler)
  }
}
</script>
